package com.huangd.platform.common.utils;


import com.huangd.platform.common.exception.RRException;

import java.util.Arrays;

public class ProcessHelper {

    public static StreamDrainer execute(String cmd, StreamDrainer drainer) throws Exception {

        String[] shell = new String[]{"sh", "-c", cmd};

        System.out.println(Arrays.toString(shell));

        Process process = Runtime.getRuntime().exec(shell);

        drainer.reset(process.getInputStream(), process.getErrorStream());
        drainer.start();

        process.waitFor();

        return drainer;
    }

    public static StreamDrainer execute(String cmd) {

        String[] shell = new String[]{"sh", "-c", cmd};

        System.out.println(Arrays.toString(shell));
        StreamDrainer drainer = new StreamDrainer();
        try {
            Process process = Runtime.getRuntime().exec(shell);


            drainer.reset(process.getInputStream(), process.getErrorStream());
            drainer.start();


            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("读取日志出错");
        }
        return drainer;
    }

    public static StreamDrainer execute(String[] cmd, StreamDrainer drainer) throws Exception {

        System.out.println(Arrays.toString(cmd));

        Process process = Runtime.getRuntime().exec(cmd);

        drainer.reset(process.getInputStream(), process.getErrorStream());
        drainer.start();

        process.waitFor();

        return drainer;
    }

    public static StreamDrainer execute(String[] cmd) throws Exception {

        System.out.println(Arrays.toString(cmd));

        Process process = Runtime.getRuntime().exec(cmd);

        StreamDrainer drainer = new StreamDrainer();
        drainer.reset(process.getInputStream(), process.getErrorStream());
        drainer.start();

        process.waitFor();

        return drainer;
    }
}
